package com.music.action.detail;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.common.ParamUtil;
import com.music.detail.service.TrackDetailService;
import com.music.domain.Commit;
import com.music.domain.Music;

/**
 * 音乐描述相关action
 * 
 * @author LFSenior
 *
 */
@SuppressWarnings("serial")
public class TrackDetailAction extends BaseAction {
	@Autowired
	TrackDetailService trackDetailService;
	
	
	/**
	 * 跳转到详情页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "trackdetail", results = { @Result(name = "trackdetail", location = "track-detail.jsp") })
	public String redicterTrackDetail() throws Exception {
		Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
		request.put(BaseKey.MUSIC_DETAIL, findMusicById(param.get("id").toString()));
		/**
		 * 根据作者查询相关音乐
		 */
		request.put(BaseKey.MUSIC_AUTHORS,findMusicByAuthorId(param.get("authorId").toString()));
		return "trackdetail";
	}
	
	/**
	 * 加载commit相关内容
	 * @return
	 * @throws Exception
	 */
	@Action(value="commit",results={@Result(name="commit",location="track-detail/commit.jsp")})
	public String redicterCommit() throws Exception{
		Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
		/**
		 * 查找评论的类容
		 */
		request.put(BaseKey.MUSIC_COMMIT,findCommitsById(param.get("id").toString()));
		return "commit";
	}
	
	
	/**
	 * 评论
	 * @return
	 */
	@Action(value="submitCommit",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String submitCommit(){
		try {
			Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
			param.put("createname", ParamUtil.buildUserName(ServletActionContext.getRequest()));
			param.put("id", ParamUtil.buildUUID());
			trackDetailService.addCommit(param);
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}
	
	/**
	 * 根据作者id查找相应的音乐最大不超过十首
	 * @param id
	 * @return
	 */
	public List<Music> findMusicByAuthorId(String authorId){
		return trackDetailService.findMusicByAuthorId(authorId);
	}
	
	
	/**
	 * 根据musicid查找对应的评论别表
	 * @param id
	 * @return
	 */
	public List<Commit> findCommitsById(String id){
		return trackDetailService.findCommitsById(id);
	}
	
	/**
	 * 根据id查询音乐
	 * @param id
	 * @return
	 */
	public Music findMusicById(String id){
		return trackDetailService.findMusicById(id);
	}
	
}
